
import re
from typing import Dict, List
from enum import Enum  # 枚举支持

# from .stream_executor import CommandType



class CommandType(Enum):
    """命令类型枚举，用于区分命令的运行特性"""
    NORMAL = "normal"           # 普通命令，会正常结束
    STREAMING = "streaming"     # 持续输出命令（如tail -f）
    LONG_RUNNING = "long_running"  # 长时间运行，可能需要特殊管理

class CommandClassifier:
    """命令类型分类器"""
    
    # 持续输出命令的模式
    STREAMING_PATTERNS = [
        r'^tail\s+.*-f',           # tail -f
        r'^ping\s+[^-]*',         # ping without -c parameter
        r'^top\s*',               # top
        r'^htop\s*',              # htop
        r'^watch\s+',              # watch command
        r'^tcpdump\s+',            # tcpdump
        r'^journalctl\s+.*-f',     # journalctl -f
        r'^docker\s+logs\s+.*-f',  # docker logs -f
        r'^kubectl\s+logs\s+.*-f', # kubectl logs -f
    ]
    
    # 长时间运行但会结束的命令
    LONG_RUNNING_PATTERNS = [
        r'^rsync\s+',              # rsync
        r'^scp\s+',                # scp
        r'^wget\s+',               # wget
        r'^curl\s+.*--progress',   # curl with progress
        r'^apt\s+install',         # apt install
        r'^yum\s+install',         # yum install
        r'^docker\s+build',        # docker build
        r'^make\s+',               # make
    ]
    
    @classmethod
    def classify_command(cls, command: str,cmd_type_input=None) -> CommandType:
        """
        分类命令类型
        
        Args:
            command: 要分类的命令
            
        Returns:
            CommandType: 命令类型
        """
        command_lower = command.lower().strip()
        
        if cmd_type_input:
            return CommandType(cmd_type_input)
        
        # 检查是否为流式命令
        for pattern in cls.STREAMING_PATTERNS:
            if re.search(pattern, command_lower):
                return CommandType.STREAMING
        
        # 检查是否为长时间运行命令
        for pattern in cls.LONG_RUNNING_PATTERNS:
            if re.search(pattern, command_lower):
                return CommandType.LONG_RUNNING
        
        return CommandType.NORMAL
    
    @classmethod
    def get_recommended_timeout(cls, command: str, command_type: CommandType) -> int:
        """获取推荐的超时时间"""
        if command_type == CommandType.STREAMING:
            return 300  # 流式命令5分钟
        elif command_type == CommandType.LONG_RUNNING:
            return 1800  # 长时间命令30分钟
        else:
            return 30   # 普通命令30秒